package com.rcl.mvvmdemo.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.rcl.mvvmdemo.lifecycle.ILifecycleOwner
import com.rcl.mvvmdemo.lifecycle.LifecycleProviders
import com.rcl.mvvmdemo.lifecycle.LifecycleState

open class BaseActivity : AppCompatActivity(), ILifecycleOwner {

    private val lifecycleProviders by lazy {
        LifecycleProviders()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleProviders.makeLifecycleState(LifecycleState.CREATE)
    }

    override fun onStart() {
        super.onStart()
        lifecycleProviders.makeLifecycleState(LifecycleState.START)
    }

    override fun onResume() {
        super.onResume()
        lifecycleProviders.makeLifecycleState(LifecycleState.RESUME)
    }

    override fun onPause() {
        super.onPause()
        lifecycleProviders.makeLifecycleState(LifecycleState.PAUSE)
    }

    override fun onStop() {
        super.onStop()
        lifecycleProviders.makeLifecycleState(LifecycleState.STOP)
    }

    override fun onDestroy() {
        super.onDestroy()
        lifecycleProviders.makeLifecycleState(LifecycleState.DESTROY)
    }

    //接口获取LifecycleProvider对象
    override fun getLifecycleProvider(): LifecycleProviders {
        return lifecycleProviders;
    }
}